package com.xmtlxx.web.controller.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xmtlxx.web.controller.oms.service.IStagingService;
import com.xmtlxx.web.controller.tms.service.IOrderService;
import com.xmtlxx.web.controller.wms.service.IPutDtlService;
import com.xmtlxx.web.controller.wms.service.IPutTaskService;
import com.xmtlxx.web.controller.wms.service.IWarehouseService;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.SysOss;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.service.ISysOssService;
import com.xmtlxx.tms.domain.vo.OrderVo;
import com.xmtlxx.wms.domain.PutTask;
import com.xmtlxx.wms.domain.bo.PutDtlBo;
import com.xmtlxx.wms.domain.bo.PutTaskBo;
import com.xmtlxx.wms.domain.vo.PutDtlVo;
import com.xmtlxx.wms.domain.vo.PutTaskVo;
import com.xmtlxx.wms.domain.vo.WarehouseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 上架任务Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "上架任务控制器", tags = {"上架任务管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/putTask")
public class PutTaskController extends BaseController {

    private final IPutTaskService iPutTaskService;

    private final IPutDtlService iPutDtlService;

    private final IWarehouseService iWarehouseService;

    private final ISysOssService iSysOssService;

    private final IStagingService stagingService;

    private final IOrderService orderService;


    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<PutTaskVo> initBean(PutTaskBo bo){
        PutTaskVo vo = iPutTaskService.queryPutTaskVoByWarehouseID(bo.getWid());
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询上架任务列表
     */
    @ApiOperation("查询上架任务列表")
//    @SaCheckPermission("wms:putTask:list")
    @GetMapping("/list")
    public TableDataInfo<PutTaskVo> list(@Validated(QueryGroup.class) PutTaskBo bo, PageQuery pageQuery) {
        bo.setDelFlag("0");
        return iPutTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出上架任务列表
     */
    @ApiOperation("导出上架任务列表")
    @SaCheckPermission("wms:putTask:export")
    @Log(title = "上架任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated PutTaskBo bo, HttpServletResponse response) {
        List<PutTaskVo> list = iPutTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "上架任务", PutTaskVo.class, response);
    }

    /**
     * 获取上架任务详细信息
     */
    @ApiOperation("获取上架任务详细信息")
//    @SaCheckPermission("wms:putTask:query")
    @GetMapping("/{id}")
    public R<PutTaskVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        PutTaskVo vo = iPutTaskService.queryById(id);
        PutDtlBo bo = new PutDtlBo();
        bo.setTaskId(id);
        bo.setDelFlag("0");

        WarehouseVo warehouseVo = iWarehouseService.queryById(vo.getWid());
        OrderVo orderVo = orderService.queryByTaskId(warehouseVo.getTaskId());

        SysOssBo orderOssBo = new SysOssBo();
        if(ObjectUtil.isNotNull(orderVo)){
            orderOssBo.setObjid(orderVo.getId());
        }

        List<PutDtlVo> putDtlVos = iPutDtlService.queryList(bo);
        vo.setItems(putDtlVos);

        vo.setBuss(warehouseVo.getBuss());
        if(warehouseVo != null){
            vo.setBuss(warehouseVo.getBuss());
        }

        return R.ok(vo);
    }

    /**
     * 查询上架任务列表
     */
    @ApiOperation("查询上架附件信息")
    @GetMapping("/getPics/{id}")
    public R<List<SysOss>> getPics(@ApiParam("主键") @NotNull(message = "主键不能为空") @PathVariable("id") String id) {
        return R.ok(iPutTaskService.getPics(id));
    }

    /**
     * 查询上架任务列表
     */
    @ApiOperation("修改保管箱号")
    @PostMapping("/updatePutDtlByBoxNo")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> updatePutDtlByBoxNo(@Validated(AddGroup.class) @RequestBody PutTaskVo vo) {
        PutTaskBo bo = new PutTaskBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iPutTaskService.updatePutDtlByBoxNo(bo));
    }

    /**
     * 重载上架任务
     */
    @ApiOperation("重载上架任务")
    @PostMapping("/refresh")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> refresh(@Validated(AddGroup.class) @RequestBody PutTaskVo vo) {
        PutTaskBo bo = new PutTaskBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iPutTaskService.refreshPutTask(bo));
    }

    /**
     * 获取上架任务详细信息
     */
    @ApiOperation("获取上架任务详细信息")
//    @SaCheckPermission("wms:putTask:query")
    @GetMapping("getTaskInfo/{id}")
    public R<PutTaskVo> getTaskInfo(@ApiParam("主键")
                                @NotNull(message = "主键不能为空")
                                @PathVariable("id") String id) {
        PutTaskVo vo = iPutTaskService.queryByTaskId(id);
        if(vo != null && StringUtils.isNotEmpty(vo.getId())){
            PutDtlBo bo = new PutDtlBo();
            bo.setTaskId(vo.getId());
            vo.setItems(iPutDtlService.queryList(bo));
        }
        return R.ok(vo);
    }

    /**
     * 新增上架任务
     */
    @ApiOperation("新增上架任务")
    @SaCheckPermission("wms:putTask:add")
    @Log(title = "上架任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PutTaskVo vo) {
        PutTaskBo bo = new PutTaskBo();
        BeanUtil.copyProperties(vo,bo);
        PutTask add = iPutTaskService.insertByBo(bo);
        if(add != null && StringUtils.isNotEmpty(add.getId())){
            iPutTaskService.pushPutTaskDatas(add.getId());
        }
        return toAjax( add != null ? 1 : 0);
    }

    /**
     * 修改上架任务
     */
    @ApiOperation("修改上架任务")
    @SaCheckPermission("wms:putTask:edit")
    @Log(title = "上架任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<PutTaskVo> edit(@Validated(EditGroup.class) @RequestBody PutTaskVo vo) {
        PutTaskBo bo = new PutTaskBo();
        BeanUtil.copyProperties(vo,bo);
        PutTaskVo item = iPutTaskService.updateByBo(bo);
        if(item != null && StringUtils.isNotEmpty(item.getId())){
            iPutTaskService.pushPutTaskDatas(item.getId());
        }
        return R.ok(item);
    }

    /**
     * 修改上架任务
     */
    @ApiOperation("修改上架任务")
    @SaCheckPermission("wms:putTask:edit")
    @Log(title = "上架任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/finish")
    @Transactional(rollbackFor = Exception.class)
    public R<PutTaskVo> finish(@Validated(EditGroup.class) @RequestBody PutTaskVo vo) {
        PutTaskBo bo = new PutTaskBo();
        bo.setStatus("1");
        BeanUtil.copyProperties(vo,bo);
        PutTaskVo item = iPutTaskService.updateByBo(bo);
        if(item != null && StringUtils.isNotEmpty(item.getId())){
            iPutTaskService.pushPutTaskDatas(item.getId());
        }
        return R.ok(item);
    }

    /**
     * 删除上架任务
     */
    @ApiOperation("删除上架任务")
    @SaCheckPermission("wms:putTask:remove")
    @Log(title = "上架任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iPutTaskService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 上架任务-拆箱
     */
    @ApiOperation("上架任务-拆箱")
    @PostMapping("/unpacking/{boxId}/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R<List<PutDtlVo>> unpacking(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String boxId,@PathVariable String id) {
        return R.ok(iPutTaskService.deleteWithBoxByIds(boxId,id));
    }
}
